﻿namespace KeRui.BLL
{
    using KeRui.Common;
    using KeRui.DAL;
    using KeRui.Model;
    using System;
    using System.Collections.Generic;
    using System.Data;

    public class SellerAD
    {
        private readonly KeRui.DAL.SellerAD dal = new KeRui.DAL.SellerAD();

        public int Add(KeRui.Model.SellerAD model)
        {
            return this.dal.Add(model);
        }

        public int CheckTime(int AdID, DateTime StartDate, DateTime EndDate, int ID)
        {
            return this.dal.CheckTime(AdID, StartDate, EndDate, ID);
        }

        public List<KeRui.Model.SellerAD> DataTableToList(DataTable dt)
        {
            List<KeRui.Model.SellerAD> list = new List<KeRui.Model.SellerAD>();
            int count = dt.Rows.Count;
            if (count > 0)
            {
                for (int i = 0; i < count; i++)
                {
                    KeRui.Model.SellerAD item = new KeRui.Model.SellerAD();
                    if (dt.Rows[i]["ID"].ToString() != "")
                    {
                        item.ID = int.Parse(dt.Rows[i]["ID"].ToString());
                    }
                    if (dt.Rows[i]["SellerID"].ToString() != "")
                    {
                        item.SellerID = int.Parse(dt.Rows[i]["SellerID"].ToString());
                    }
                    item.SellerName = dt.Rows[i]["SellerName"].ToString();
                    if (dt.Rows[i]["AdID"].ToString() != "")
                    {
                        item.AdID = int.Parse(dt.Rows[i]["AdID"].ToString());
                    }
                    item.AdName = dt.Rows[i]["AdName"].ToString();
                    if (dt.Rows[i]["AdWidth"].ToString() != "")
                    {
                        item.AdWidth = int.Parse(dt.Rows[i]["AdWidth"].ToString());
                    }
                    if (dt.Rows[i]["AdHight"].ToString() != "")
                    {
                        item.AdHight = int.Parse(dt.Rows[i]["AdHight"].ToString());
                    }
                    if (dt.Rows[i]["AdType"].ToString() != "")
                    {
                        item.AdType = int.Parse(dt.Rows[i]["AdType"].ToString());
                    }
                    item.AdText = dt.Rows[i]["AdText"].ToString();
                    item.AdUrl = dt.Rows[i]["AdUrl"].ToString();
                    if (dt.Rows[i]["StartDate"].ToString() != "")
                    {
                        item.StartDate = DateTime.Parse(dt.Rows[i]["StartDate"].ToString());
                    }
                    if (dt.Rows[i]["EndDate"].ToString() != "")
                    {
                        item.EndDate = DateTime.Parse(dt.Rows[i]["EndDate"].ToString());
                    }
                    if (dt.Rows[i]["State"].ToString() != "")
                    {
                        item.State = int.Parse(dt.Rows[i]["State"].ToString());
                    }
                    if (dt.Rows[i]["CreateTime"].ToString() != "")
                    {
                        item.CreateTime = DateTime.Parse(dt.Rows[i]["CreateTime"].ToString());
                    }
                    item.CreateIP = dt.Rows[i]["CreateIP"].ToString();
                    list.Add(item);
                }
            }
            return list;
        }

        public void Delete(int ID)
        {
            this.dal.Delete(ID);
        }

        public List<KeRui.Model.SellerAD> GetList(int SiteID, string adname)
        {
            string strWhere = " SiteID=" + SiteID + "";
            if (adname != "")
            {
                strWhere = strWhere + " AND AdName like '%" + adname + "%'";
            }
            string strOrder = " ID asc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("SellerAD", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public List<KeRui.Model.SellerAD> GetList(int SiteID, string AdName, int AdID)
        {
            string strWhere = string.Concat(new object[] { " SiteID=", SiteID, " and adid=", AdID });
            if (AdName != "")
            {
                strWhere = strWhere + " AND AdName like '%" + AdName + "%'";
            }
            string strOrder = " EndDate desc";
            int pageSize = -1;
            int pageIndex = -1;
            DataSet set = new KeRui.DAL.Common().GetList("SellerAD", pageSize, pageIndex, strWhere, strOrder);
            return this.DataTableToList(set.Tables[0]);
        }

        public KeRui.Model.SellerAD GetModel(int ID)
        {
            return this.dal.GetModel(ID);
        }

        public KeRui.Model.SellerAD GetModelByCache(int ID)
        {
            string cacheKey = "SellerADModel-" + ID;
            object cache = DataCache.GetCache(cacheKey);
            if (cache == null)
            {
                try
                {
                    cache = this.dal.GetModel(ID);
                    if (cache != null)
                    {
                        int configInt = ConfigHelper.GetConfigInt("ModelCache");
                        DataCache.SetCache(cacheKey, cache, DateTime.Now.AddMinutes((double) configInt), TimeSpan.Zero);
                    }
                }
                catch
                {
                }
            }
            return (KeRui.Model.SellerAD) cache;
        }

        public DataSet GetSelect()
        {
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("SellerAD ", "", " ID ", "ID,AdName");
        }

        public DataSet GetSelect1()
        {
            string strWhere = " ";
            string strOrder = " SellerName asc";
            KeRui.DAL.Common common = new KeRui.DAL.Common();
            return common.GetSelect("Seller", strWhere, strOrder, "SellerID,SellerName");
        }

        public DataSet GetSpaceTime(int AdID)
        {
            return this.dal.GetSpaceTime(AdID);
        }

        public int Update(KeRui.Model.SellerAD model)
        {
            return this.dal.Update(model);
        }
    }
}

